﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.DAO;
using ESHOP.ADMIN.Common;
using ESHOP.Domain;
using ESHOP.UTIL;
namespace ESHOP.ADMIN
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        private readonly DanhMucDAO _danhmuc = new DanhMucDAO();
        private readonly SanPhamDAO _sanpham = new SanPhamDAO();
        private readonly QuocGiaDAO _quocgia = new QuocGiaDAO();
        public static String _msp;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadStaticReferences();
                Bindataforgird();
            }
        }


      
        private void LoadStaticReferences()
        {
            CommonFunc.BindDropDownList(ddlloaisanpham, _danhmuc.GetList(), "TENDM", "ID", true);
            CommonFunc.BindDropDownList(ddlxuatxu, _quocgia.GetList(), "TENQUOCGIA", "ID", true);
           // CommonFunc.BindDropDownList(ddlCompanies, new DoanhNghiepDAO().GetList(), "TenDoanhNghiep", "MaDoanhNghiep", true);
            cbshow.Checked = true;
        }
        private void Bindataforgird()
        {
           
            var listsanpham = _sanpham.GetList();
            GVProducts.DataSource = listsanpham;
            GVProducts.DataBind();
        }
      

        protected void GVProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                var masp = e.CommandArgument.ToString();

                switch (e.CommandName)
                {
                    case "Active":
                        if (!string.Empty.Equals(masp))
                        {
                            var sp = _sanpham.Get(masp);
                            if (sp != null)
                            {
                                var result = _sanpham.ActiveProduct(sp);
                                if (result == 1)
                                {
                                    Bindataforgird();
                                    lbThongBao.Text = "Đã cho sản phẩm hiển thị lên web site";
                                    updateinfor.Update();
                                    updategrid.Update();
                                }
                                else
                                {
                                    lbThongBao.Text = "Lỗi xảy ra";
                                }
                            }

                        }
                        break;
                    case "DeActive":
                        if (!string.Empty.Equals(masp))
                        {
                            var sp = _sanpham.Get(masp);
                            if (sp != null)
                            {
                                var result = _sanpham.DeActiveProduct(sp);
                                if (result == 1)
                                {
                                    Bindataforgird();
                                    lbThongBao.Text = "Đã không cho sản phẩm hiển thị lên web site";
                                    updategrid.Update();
                                }
                                else
                                {
                                    lbThongBao.Text = "Lỗi xảy ra";
                                }
                                updateinfor.Update();
                            }
                           
                        }
                        break;
                }
                Bindataforgird();
            }
            catch
            {
                
                return;
            }
            
        }

        private void SetToForm(SANPHAM sp)
        {
          
            txttensp.Text = sp.TENSANPHAM;
            var index = ddlloaisanpham.Items.FindByValue(sp.MADANHMUC.ToString());
            ddlloaisanpham.SelectedIndex = ddlloaisanpham.Items.IndexOf(index);
            if (sp.MACONGTY != null)
            {
                index = ddlCompanies.Items.FindByText(sp.MACONGTY.ToString());
                ddlCompanies.SelectedIndex = ddlCompanies.Items.IndexOf(index);
            }
            if (sp.MAQUOCGIA != null)
            {
                index = ddlxuatxu.Items.FindByValue(sp.MAQUOCGIA.ToString());
                ddlxuatxu.SelectedIndex = ddlxuatxu.Items.IndexOf(index);
            }
           
            txtgia.Text = sp.GIA.ToString();
            cbshow.Checked = sp.ISSHOW.HasValue ? sp.ISSHOW.Value : false;
            updateinfor.Update();
        }
        private void ClearForm()
        {
            txttensp.Text = "";
           
            ddlloaisanpham.SelectedIndex = 0;
           
            ddlCompanies.SelectedIndex = 0;
           
            ddlxuatxu.SelectedIndex =0;
            txtgia.Text = "";
            cbshow.Checked =false;

        }

       
        protected void btnsave_Click(object sender, EventArgs e)
        {
            var obj = _sanpham.Get(_msp);
            if (obj != null)
            {
                obj.ISSHOW = cbshow.Checked ? true : false;
                _sanpham.Update(obj);
                ClearForm();
                GVProducts.DataSource = Search();
                GVProducts.DataBind();
                updategrid.Update();

                
            }
        }

        protected void btnfilter_Click(object sender, EventArgs e)
        {
            GVProducts.DataSource = Search();
            GVProducts.DataBind();
            updategrid.Update();
        }
        protected List<SANPHAM> Search( )
        {
            String Tensp = txttensp.Text.Trim();
            String loaisp = ddlloaisanpham.SelectedValue;
            String congty = ddlCompanies.SelectedValue;
            String xuatxu = ddlxuatxu.SelectedValue;
            bool ishow=cbshow.Checked?true:false;
            decimal gia;
            if (CheckInterger(txtgia.Text.Trim())) { gia = decimal.Parse(txtgia.Text.Trim()); }
            else { gia = 0; }
                 return _sanpham.Search(Tensp, loaisp, congty, xuatxu, gia, ishow);
            
        }
        public bool CheckInterger(String chuoi)
        {
            try
            {
                decimal a = decimal.Parse(chuoi);
                return true;
            }
            catch
            {
                return false;
            }
        }

        protected void GVProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
            GVProducts.PageIndex = e.NewPageIndex;
            GVProducts.DataBind();
        }
    }
}